<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="采购单号">
              <a-input placeholder="请输入采购单号" v-model="queryParam.purchaseNumber"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="物流单号">
              <a-input placeholder="请输入物流单号" v-model="queryParam.logisticsNumber"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="收货仓库">
              <j-search-select-tag
                v-model="queryParam.warehouseId"
                dict="hz_wms_warehouse_info,name,id,wms,type=1 and status='Y'"
                placeholder="请选择收货仓库"
              />
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="所属组织">
                <a-input placeholder="请输入所属组织" v-model="queryParam.purchaseDept"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="货架编码">
                <a-input placeholder="请输入货架编码" v-model="queryParam.shelvesCode"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="入库状态">
                <j-dict-select-tag
                  placeholder="请选择入库状态"
                  v-model="queryParam.entryStatus"
                  dictCode="wms_entry_status"
                ></j-dict-select-tag>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="所属公司">
                <a-input placeholder="请输入所属公司" v-model="queryParam.purchaseCompany"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="处理人">
                <a-input placeholder="请输入处理人" v-model="queryParam.handling"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="理货时间">
                <a-range-picker
                  v-model="queryParam.createTimeRange"
                  style="width: 100%"
                  valueFormat="YYYY-MM-DD"
                  @change="effectTimeChange"
                ></a-range-picker>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="primary" icon="download" @click="handleExportXls('理货管理')">导出数据</a-button>
    </div>
    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <!-- 采购单号 -->
        <span slot="purchaseNumberSlot" slot-scope="text, record">
          {{ record.purchaseNumber }}<span v-if="record.putchaseUser" style="margin-left: 5px">[{{ record.putchaseUser }}]</span>
        </span>
        <!-- 收货仓库 -->
        <span slot="warehouseIdSlot" slot-scope="text, record">
          {{ record.warehouseType_dictText }} - {{ record.warehouseId_dictText }}
        </span>
        <!-- 理货时间 -->
        <span slot="tallyingDateSlot" slot-scope="text, record">
          {{ record.createTime }}<span v-if="record.createBy_dictText" style="margin-left: 5px">[{{ record.createBy_dictText }}]</span>
        </span>
        <span
          slot="action"
          slot-scope="text, record"
          style="display: flex; align-items: center; justify-content: center"
        >
          <span v-if="!record.purchaseNumber">
            <a @click="handleMatchPurchase(record)">匹配采购</a>
            <a-divider type="vertical" />
          </span>
          <a><j-operate-log :id="record.id"></j-operate-log></a>
        </span>
      </a-table>
    </div>

    <!-- 匹配采购模态框 -->
    <a-modal :visible="matchVisible" :confirm-loading="matchConfirmLoading" @ok="handleMatchConfirm" @cancel="matchVisible = false">
      <template slot="title">
        <span>采购单匹配</span>
        <span class="match-subtitle">将物流单号同步至采购单</span>
      </template>
      <j-form-container>
        <a-form-model ref="form" :model="matchForm" :rules="validatorRules" slot="detail">
          <a-row>
            <a-col :span="24">
              <a-form-model-item label="物流单号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <span>{{ matchForm.logisticsNumber }}</span>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="对应采购单" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="purchaseNumber">
                <a-input v-model="matchForm.purchaseNumber" placeholder="请输入采购单号" />
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </j-form-container>
    </a-modal>
  </a-card>
</template>
<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { filterObj } from '@/utils/util'
import { postAction } from '@/api/manage'
export default {
  name: 'TallyingInfoList',
  mixins: [JeecgListMixin, mixinDevice],
  data() {
    return {
      description: '理货管理管理页面',
      queryParam: {
        createTimeRange: [],
      },
      // 表头
      columns: [
        {
          title: 'NO',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '采购单号',
          align: 'center',
          dataIndex: 'purchaseNumber',
          scopedSlots: { customRender: 'purchaseNumberSlot' },
        },
        {
          title: '物流单号',
          align: 'center',
          dataIndex: 'logisticsNumber',
        },
        {
          title: '货架编码',
          align: 'center',
          dataIndex: 'shelvesCode',
        },
        {
          title: '收货仓库',
          align: 'center',
          dataIndex: 'warehouseId_dictText',
          scopedSlots: { customRender: 'warehouseIdSlot' },
        },
        {
          title: '入库状态',
          align: 'center',
          dataIndex: 'entryStatus_dictText',
          customCell: (record, index) => {
            if (record.entryStatus === 1) {
              return { style: { color: '#52C41A' } }
            }
          },
        },
        {
          title: '所属公司',
          align: 'center',
          dataIndex: 'purchaseCompany',
        },
        {
          title: '所属组织',
          align: 'center',
          dataIndex: 'purchaseDept',
        },
        {
          title: '理货时间',
          align: 'center',
          dataIndex: 'tallyingDate', 
          scopedSlots: { customRender: 'tallyingDateSlot' },
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      dictOptions: {},
      superFieldList: [],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      matchVisible: false, // 控制匹配采购窗口的显示状态
      matchConfirmLoading: false, //  匹配采购模态框按钮loading
      matchForm: {},
      validatorRules: {
        purchaseNumber: [{ required: true, message: '请输入采购单号' }],
      },
      url: {
        list: '/wms/tallyManage/tallyingInfo/list',
        delete: '/wms/tallyManage/tallyingInfo/delete',
        deleteBatch: '/wms//tallyManage/tallyingInfo/deleteBatch',
        exportXlsUrl: '/wms//tallyManage/tallyingInfo/exportXls',
        importExcelUrl: '/wms/tallyManage/tallyingInfo/importExcel',
        purchaseMatching: '/wms/tallyManage/tallyingInfo/purchaseMatching',
        getLog: '/wms/tallyManage/tallyingInfo/getLog',
      },
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    //选择理货时间
    effectTimeChange(date, dateString) {
      this.queryParam.createTime_begin = dateString[0] || ''
      this.queryParam.createTime_end = dateString[1] || ''
    },
    //采购单匹配
    handleMatchPurchase(record) {
      this.matchForm = {
        id: record.id,
        logisticsNumber: record.logisticsNumber,
        purchaseNumber: '',
      }
      this.matchVisible = true
    },
    //获取查询条件
    getQueryParams() {   
      let sqp = {}
      if (this.superQueryParams) {
        sqp['superQueryParams'] = encodeURI(this.superQueryParams)
        sqp['superQueryMatchType'] = this.superQueryMatchType
      }
      var param = Object.assign(sqp, this.queryParam, this.isorter, this.filters)
      param.field = this.getQueryField()
      param.pageNo = this.ipagination.current
      param.pageSize = this.ipagination.pageSize
      delete param.createTimeRange
      return filterObj(param)
    },
    //采购单匹配
    handleMatchConfirm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.matchConfirmLoading = true
          postAction(this.url.purchaseMatching,this.matchForm).then(res=>{
            if(res.success){
              this.$message.success(res.message)
              this.matchVisible = false
              this.$refs.form.clearValidate()    //清除表单校验提示信息
              this.loadData()
            }else{
              this.$message.error(res.message)
            }
          }).finally(()=>{
            this.matchConfirmLoading = false
          }) 
        }
      })
    },
  },
}
</script>
<style lang="less" scoped>
@import '~@assets/less/common.less';
.match-subtitle {
  font-size: 13px;
  margin-left: 10px;
  color: #0000006d;
}

</style>